<?php
require_once dirname(__FILE__) . '/common.inc.php';
require_once dirname(__FILE__) . "/DbUtils.class.php";

header('Content-Type: text/plain');

$id = json_decode(MiscUtils::getParam('i', '[]'));
$status = json_decode(MiscUtils::getParam('s', '[]'));
$lost = json_decode(MiscUtils::getParam('l', '[]'));

$myPdo = DbUtils::createPdoInst();
$cond_vals = new stdClass();
$cond_vals->c = '(';
$cond_vals->v = array();
foreach ($id as $idx=>$tmp) {
  $cond_vals->c .= ('deal_track_id = :v' . ($idx + 1));
  $cond_vals->v[':v' . ($idx + 1)] = $tmp;
  if ($idx < count($id) - 1) {
    $cond_vals->c .= ' OR ';
  }
}
if ($cond_vals->c == '(') {
  $cond_vals->c = '';
} else {
  $cond_vals->c .= ')';
}

if (count($status) > 0) {
  $cond_vals->c .= ($cond_vals->c == '') ? '(deal_track_id = -1) AND (' : ' AND (';
  foreach ($status as $idx=>$tmp) {
    $cond_vals->c .= ('delivery_status = :v' . (count($id) + $idx + 1));
    $cond_vals->v[':v' . (count($id) + $idx + 1)] = $tmp;
    if ($idx < (count($status) - 1)) {
      $cond_vals->c .= ' OR ';
    }
  }
  $cond_vals->c .= ')';
}

if (count($lost) > 0) {
  $cond_vals->c .= ($cond_vals->c == '') ? '(deal_track_id = -1) AND (delivery_status = -1) AND (' : ' AND (';
  foreach ($lost as $idx=>$tmp) {
    $cond_vals->c .= ('lost_item_claim = :v' . (count($id) + count($status) + $idx + 1));
    $cond_vals->v[':v' . (count($id) + count($status) + $idx + 1)] = $tmp;
    if ($idx < (count($lost) - 1)) {
      $cond_vals->c .= ' OR ';
    }
  }
  $cond_vals->c .= ')';
}
$result = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'orders_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;

date_default_timezone_set('Pacific/Auckland');

/** PHPExcel */
require_once 'libs/phpexcel/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("hhcode.com")
               ->setLastModifiedBy("hhcode.com")
               ->setTitle("Client List")
               ->setSubject("Client List")
               ->setDescription("Client list")
               ->setKeywords("hhcode.com")
               ->setCategory("Office 2007 XLSX Document");

               // Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Client List');
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'First Name')
            ->setCellValue('B1', 'Last Name!')
            ->setCellValue('C1', 'Street')
            ->setCellValue('D1', 'Street')
            ->setCellValue('E1', 'Street')
            ->setCellValue('F1', 'Suburb')
            ->setCellValue('G1', 'Town/City')
            ->setCellValue('H1', 'Post Code')
            ->setCellValue('I1', 'Phone')
            ->setCellValue('J1', 'Order ID')
            ->setCellValue('K1', 'Quantity');

foreach ($result as $idx=>&$item) {
  $objPHPExcel->setActiveSheetIndex(0)
              ->setCellValue('A' . ($idx + 2), $item->first_name)
              ->setCellValue('B' . ($idx + 2), $item->last_name)
              ->setCellValue('C' . ($idx + 2), $item->address1)
              ->setCellValue('D' . ($idx + 2), $item->address2)
              ->setCellValue('E' . ($idx + 2), $item->address3)
              ->setCellValue('F' . ($idx + 2), $item->suburb)
              ->setCellValue('G' . ($idx + 2), $item->city)
              ->setCellValue('H' . ($idx + 2), $item->postcode)
              ->setCellValue('I' . ($idx + 2), $item->phone)
              ->setCellValue('J' . ($idx + 2), $item->order_id)
              ->setCellValue('K' . ($idx + 2), $item->quantity);
}

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="www.hhcode.com_' . date('Y_m_d') . '__________' . count($result) . '_items.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>